package com.apartment.controller;


import com.apartment.auto.JSONAuthentication;
import com.apartment.entity.ApHouse;
import com.apartment.entity.ApRent;
import com.apartment.entity.HouseType;
import com.apartment.mapper.ApHouseMapper;
import com.apartment.mapper.ApRentMapper;
import com.apartment.mapper.ApUserMapper;
import com.apartment.mapper.HouseTypeMapper;
import com.apartment.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author apartment
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/ap-rent")
public class ApRentController extends JSONAuthentication {
    @Autowired
    ApRentMapper apRentMapper;
    @Autowired
    ApUserMapper apUserMapper;
    @Autowired
    ApHouseMapper apHouseMapper;
    @Autowired
    HouseTypeMapper houseTypeMapper;
    @ApiOperation("添加住房记录")
    @PostMapping("/add")
    public void addHouseRecoder(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int  userId, @RequestParam("houseId") int houseId,@RequestParam("time") int time )throws Exception{
        ApRent  apRent=new ApRent();
        apRent.setUserId(userId);
        apRent.setHouseId(houseId);
        apRent.setTime(time);
        ApHouse house=apHouseMapper.selectById(apRent.getHouseId());
        if(house.getStatus()=="已有人居住"){
            Result error = Result.error().message("已有人居住");
            this.WriteJSON(request, response, error);
        }else{
            house.setStatus("已有人居住");
            HouseType houseType = houseTypeMapper.selectById(house.getType());
            float jiage=houseType.getPrice();
            float totalPrice=jiage*time;
            apRent.setPrice(totalPrice);
            if(apRentMapper.insert(apRent)>0&&apHouseMapper.updateById(house)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }

        }
    }
    @ApiOperation(value = "修改用户住房记录(退房)")
    @PostMapping("/updRentStatus")
    public void updRentStatus(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id)throws Exception{
        ApRent apRent = apRentMapper.selectById(id);
        apRent.setStatus("退房");
        ApHouse apHouse = apHouseMapper.selectById(apRent.getHouseId());
        apHouse.setStatus("空闲");
        if(apRentMapper.updateById(apRent)>0&&apHouseMapper.updateById(apHouse)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除住房记录")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=apRentMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询全部租房记录")
    @PostMapping("/findAllRent")
    public void findAllRent(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<ApRent> shUserPage=new Page<>(current,size);
        Page<ApRent> shUserPages=apRentMapper.selectPage(shUserPage,null);
        List<ApRent> records = shUserPages.getRecords();
        int total=apRentMapper.selectCount(null);
       for(ApRent a :records){
           a.setApUser(apUserMapper.selectById(a.getUserId()));
           a.setApHouse(apHouseMapper.selectById(a.getHouseId()));

       }
        if(records.size()>0){
            Result ok = Result.ok().data("APRent",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("无租房记录");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询全部租房记录通过用户id")
    @PostMapping("/findAllRentByUserId")
    public void findAllRentByUserId(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("userId") int userId)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<ApRent> shUserPage=new Page<>(current,size);
        Page<ApRent> shUserPages=apRentMapper.selectPage(shUserPage,wrapper);
        List<ApRent> records = shUserPages.getRecords();
        int total=apRentMapper.selectCount(wrapper);
        for(ApRent a :records){
            a.setApUser(apUserMapper.selectById(a.getUserId()));
            a.setApHouse(apHouseMapper.selectById(a.getHouseId()));

        }
        if(records.size()>0){
            Result ok = Result.ok().data("APRent",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("无租房记录");
            this.WriteJSON(request, response, error);
        }
    }

}

